/* 创建集合 */
function fn1() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017";

  MongoClient.connect(url, function (err, db) {
    if (err) throw err;

    console.log("MongoDB服务已连接");
    var dbase = db.db("my_express_server");

    dbase.createCollection("site", function (err, res) {
      if (err) throw err;

      console.log("创建集合!");
      db.close();
    });
  });
}
// fn1();

/* 插入一条数据 */
function fn2() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    var dbo = db.db("my_express_server");

    var myobj = { name: "菜鸟教程", url: "www.runoob.com" };
    dbo.collection("site").insertOne(myobj, function (err, res) {
      if (err) throw err;

      console.log("文档插入成功");
      db.close();
    });
  });
}
// fn2();

/* 插入多条数据 */
function fn3() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;

    // 连接指定的数据库
    var dbObj = conn.db("my_express_server");

    var arr = [
      { name: "菜鸟工具", url: "https://c.runoob.com", type: "cn" },
      { name: "Google", url: "https://www.google.com", type: "en" },
      { name: "Facebook", url: "https://www.google.com", type: "en" },
    ];

    dbObj.collection("site").insertMany(arr, function (err, res) {
      if (err) throw err;

      // 插入了N条数据
      console.log("插入的文档数量为: " + res.insertedCount);
      conn.close();
    });
  });
}
// fn3();

/* 查询集合中的所有数据 */
function fn4() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;
    var dbObj = conn.db("my_express_server");

    dbObj
      .collection("site")
      .find({})
      .toArray(function (err, result) {
        // 返回集合中所有数据
        if (err) throw err;

        console.log(result);
        conn.close();
      });
  });
}
// fn4();

/* 按条件查询 */
function fn5() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;

    var dbObj = conn.db("my_express_server");

    dbObj
      .collection("site")
      .find({ name: "菜鸟教程" })
      .toArray(function (err, result) {
        if (err) throw err;

        console.log(result);
        conn.close();
      });
  });
}
// fn5();

/* 更新数据 */
function fn6() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;
    var dbObj = conn.db("my_express_server");

    dbObj
      .collection("site")
      .updateOne(
        { name: "菜鸟教程" },
        { $set: { url: "https://www.runoob.com.cn" } },
        function (err, res) {
          conn.close();

          if (err) throw err;
          console.log("文档更新成功");
        }
      );
  });
}
// fn6();

/* 更新多条数据记录 */
function fn7() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;
    var dbObj = conn.db("my_express_server");

    dbObj
      .collection("site")
      .updateMany(
        { type: "en" },
        { $set: { url: "https://www.dacainiao.com" } },
        function (err, res) {
          conn.close();

          if (err) throw err;
          console.log(res.modifiedCount, "条记录已经修改");
        }
      );
  });
}
// fn7();

/* 删除一条数据 */
function fn8() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;
    var dbObj = conn.db("my_express_server");

    dbObj
      .collection("site")
      .deleteOne({ name: "菜鸟教程" }, function (err, result) {
        conn.close();

        if (err) throw err;
        console.log("文档删除成功");
      });
  });
}
// fn8();

/* 删除多条数据 */
function fn9() {
  var MongoClient = require("mongodb").MongoClient;
  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function (err, conn) {
    if (err) throw err;
    var dbObj = conn.db("my_express_server");

    dbObj.collection("site").deleteMany({ type: "en" }, function (err, result) {
      conn.close();
      if (err) throw err;
      console.log(result);
    });
  });
}
// fn9();
